package com.abcdefg.rabbitmqextensionsdemo.config;


import com.abcdefg.rabbitmqextensionsdemo.constants.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    //消息确认
    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constant.DIRECT_QUEUE_NAME).build();
    }

    @Bean("ackExchange")
    public DirectExchange ackExchange() {
        return ExchangeBuilder.directExchange(Constant.DIRECT_EXCHANGE_NAME).build();
    }
    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackExchange") DirectExchange directExchange,
                              @Qualifier("ackQueue") Queue ackQueue) {
        return BindingBuilder.bind(ackQueue).to(directExchange).with("ack");
    }

    //持久化

    @Bean("preQueue")
    public Queue prequeue() {
        //队列持久化
//        return QueueBuilder.durable(Constant.PRES_QUEUE_NAME).build();
        //队列非持久化
        return QueueBuilder.nonDurable(Constant.PRES_QUEUE_NAME).build();
    }

    @Bean("preExchange")
    public DirectExchange preExchange() {
        //交换机持久化
//        return ExchangeBuilder.directExchange(Constant.PRES_EXCHANGE_NAME).build();
        //交换机非持久化
        return ExchangeBuilder.directExchange(Constant.PRES_EXCHANGE_NAME).durable(false).build();
    }

    @Bean
    public Binding presbinding(@Qualifier("preExchange") DirectExchange preExchange,
                               @Qualifier("preQueue") Queue preQueue) {
        return BindingBuilder.bind(preQueue).to(preExchange).with("pre");
    }

    //发送方确认
    @Bean("confirmQueue")
    public Queue confirmqueue() {
        return QueueBuilder.durable(Constant.CONFIRM_QUEUE).build();
    }

    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        return ExchangeBuilder.directExchange(Constant.CONFIRM_EXCHANGE).build();
    }

    @Bean("confirmBind")
    public Binding confirmBind(@Qualifier("confirmExchange") DirectExchange directExchange,
                               @Qualifier("confirmQueue") Queue confirmQueue) {
        return BindingBuilder.bind(confirmQueue).to(directExchange).with("confirm");
    }


    //重试机制
    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constant.RETRY_QUEUE).build();
    }

    @Bean("retryExchange")
    public DirectExchange retryExchange() {
        return ExchangeBuilder.directExchange(Constant.RETRY_EXCHANGE).build();
    }

    @Bean("retryBind")
    public Binding retryBind(@Qualifier("retryExchange") DirectExchange retryExchange,
                               @Qualifier("retryQueue") Queue retryQueue) {
        return BindingBuilder.bind(retryQueue).to(retryExchange).with("retry");
    }


    //TTL机制
    @Bean("ttlQueue")
    public Queue ttlQueue() {
        return QueueBuilder.durable(Constant.TTL_QUEUE).build();
    }
    @Bean("ttlQueue2")
    public Queue ttlQueue2() {
        return QueueBuilder.durable(Constant.TTL_QUEUE2).ttl(20000).build();
    }

    @Bean("ttlExchange")
    public DirectExchange ttlExchange() {
        return ExchangeBuilder.directExchange(Constant.TTL_EXCHANGE).build();
    }

    @Bean("ttlBind")
    public Binding ttlBind(@Qualifier("ttlExchange") Exchange Exchange,
                             @Qualifier("ttlQueue") Queue Queue) {
        return BindingBuilder.bind(Queue).to(Exchange).with("ttl").noargs();
    }
    @Bean("ttlBind2")
    public Binding ttlBind2(@Qualifier("ttlExchange") Exchange Exchange,
                           @Qualifier("ttlQueue2") Queue Queue) {
        return BindingBuilder.bind(Queue).to(Exchange).with("ttl").noargs();
    }

    //事务
    @Bean("traceQueue")
    public Queue traceQueue(){
        return QueueBuilder.durable(Constant.TRACE_QUEUE).build();
    }
}
